package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyVerifyDao;
import com.boss.salary.dao.IPersonExtDao;
import com.boss.salary.dao.IPersonOperateDao;
import com.boss.salary.entity.PersonExt;
import com.boss.salary.entity.PersonInfo;
import com.boss.salary.entity.PersonOperate;
import com.boss.salary.service.IPersonExtService;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class PersonExtServiceImpl implements IPersonExtService {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(PersonExtServiceImpl.class);

    @Autowired
    private IPersonExtDao dao;
    @Autowired
    private IPersonOperateDao iPersonOperateDao;
    @Autowired
    private IAgencyVerifyDao agencyVerifyDao;

    @Override
    public PageDTO<PersonExt> queryPersonExtPage(PaginationDTO paginationDTO) throws AppRuntimeException {
        List<PersonExt> list = dao.queryPersonExtPage(paginationDTO);
        int total = dao.queryPersonExtTotal();
        PageDTO<PersonExt> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }




    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePersonExt(PersonExt personExt) {
        personExt.setId(SysUtils.generateGuid());
        personExt.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personExt.setIsDeleted(SystemDictConstant.IS_FALSE);
        dao.savePersonExt(personExt);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonExt(PersonExt personExt) {
        personExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        dao.updatePersonExt(personExt);
        dao.updateOperate(personExt);
        dao.updateBegin(personExt);
        dao.updateSubmit(personExt);
    }

    /**
     * 更新 工资是否停发数据
     * @param personInfo
     * @return
     */
       @Override
       @Transactional(rollbackFor = Exception.class)
    public void updateIsStopSalary(PersonInfo personInfo) {
           //查询最新人员信息
           PersonOperate po=iPersonOperateDao.selectHistoryOperateByPersonId(personInfo.getId(),"1");
           //若未查到不处理
           if(!SysUtils.isNullOrEmpty(po)){
               if(SystemDictConstant.IS_TRUE.equals(personInfo.getIsStopSalary())) {
                   po.setOperateCode(SystemDictConstant.STOP_PERSON);
               }else{
                   po.setOperateCode(SystemDictConstant.CANCEL_STOP);
               }
               po.setVerifyState("2");
               po.setIsReal(1);
               po.setPersonId(personInfo.getId());
               po.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
               po.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
               Map<String,Object> stateMap = agencyVerifyDao.selectStateByAgencyId(personInfo.getAgencyId(),"salary_month");
//        当月月份
               String salaryMonth = String.valueOf(stateMap.get("salary_month"));
               po.setSalaryMonth(salaryMonth);
               po.setId(SysUtils.generateGuid());
               iPersonOperateDao.savePersonOperate(po);
           }

           dao.updateIsStopSalary(personInfo);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePersonExt(String id) {
            dao.deletePersonExt(id);
    }

    /**
     * 据区划号 单位id 查找单位的人数
     * @param code
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> queryPersons(String code, String id) {
        List<Map<String, Object>> list = dao.queryPersons(code,id);
        return list;
    }

    /**
     * 机构人员维护修改排序
     * @param personExt
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonExts(List<PersonExt> personExt) {
        dao.updatePersonExts(personExt);
        dao.updateOperates(personExt);
        dao.updateBegins(personExt);
        dao.updateSubmits(personExt);

    }
    /**
     * 机构人员维护修改人员编号
     * @param personExt
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonnelCodeList(List<PersonExt> personExt) {
        dao.updatePersonExtPersonnelCode(personExt);
        dao.updateOperatePersonnelCode(personExt);
        dao.updateBeginPersonnelCode(personExt);
        dao.updateSubmitPersonnelCode(personExt);
        dao.updateHistoryPersonnelCode(personExt);

    }


}
